#include "cpomqtt.h"
#include "cpoproc.h"
#include "es_print.h"

bool clean_session = true;
bool unclean_session = false;

struct mosquitto *mqtt_cpo;

/* mqtt publish 回调 */
void cpo_mqtt_pub_cb(struct mosquitto *mosq, void *userdata, int mid)
{
	ES_PRT_INFO("AirCond publish callback \n");	
}

/* mqtt subscribe 回调 */
void cpo_mqtt_sub_cb(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
//	printf("Subscribe callback, mid = %d \n", mid);
}

/* mqtt connect 回调 */
void cpo_mqtt_conn_cb(struct mosquitto *mosq, void *userdata, int rc)
{	//rc=0, 连接成功
	//mosquitto_connack_string()获取rc取值的含义
	if(!rc){
		ES_PRT_INFO("Connect callback \n");
    }else{
        ES_PRT_INFO("Connect failed\n");
    }
}

void cpo_mqtt_log_cb(struct mosquitto *mosq, void *userdata, int level, const char *str)
{
	ES_PRT_INFO("%s \n", str);
}

/* mqtt message回调 */
void cpo_mqtt_msg_cb(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *msg)
{
	ES_PRT_DEBUG("rcv msg->topic = %s \n", (char *)msg->topic);
//	ES_PRT_DEBUG("msg->payload = %s \n", (char *)msg->payload);

	if(strstr((char *)msg->topic, "command") != NULL){
    	cpo_mqtt_cmd_msg_proc((char *)msg->payload, (char *)msg->topic);
	}else{
		cpo_mqtt_msg_proc((char *)msg->payload);
	}

}